﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Extensil.Processor.Extensibility;
using Extensil.Common;
using System.IO;
using System.Configuration;
using Vibz.Licensing;

namespace Extensil
{
    
    static class Program
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew = true;
            using(Mutex mutex = new Mutex(true, "Extensil", out createdNew))
            {
                if(createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    try
                    {
                        using(StreamWriter writer = new StreamWriter("extensilConsole.log"))
                        {
                            bool log = false;
                            string allowLogging = ConfigurationManager.AppSettings["AllowLogging"];
                            if(!String.IsNullOrEmpty(allowLogging) && bool.TryParse(allowLogging, out log) && log)
                                Console.SetOut(writer);
                            ConfigSystem.Install();

                            string masterKey = ConfigurationManager.AppSettings["MasterKey"];
                            if(masterKey != null && masterKey.Equals("vw20102011"))
                                Application.Run(new ExtensilManager());
                            else
                            {
                                TrialMaker t = new TrialMaker("Extensil1.0.0.1", Application.StartupPath + "\\RegFile.reg",
                                    Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\TMSetp.dbf",
                                    "Phone: +91 7829486777\nEmail: amit.vibhuti@rediffmail.com", 30, 30, "321");

                                byte[] MyOwnKey = { 97, 250,  1,  5,  84, 21,   7, 63,
                                4,  54, 87, 56, 123, 10,   3, 62,
                                7,   9, 20, 36,  37, 21, 101, 57};

                                t.TripleDESKey = MyOwnKey;

                                TrialMaker.RunTypes RT = t.ShowDialog();

                                bool is_trial;
                                if(RT != TrialMaker.RunTypes.Expired)
                                {
                                    if(RT == TrialMaker.RunTypes.Full)
                                        is_trial = false;
                                    else
                                        is_trial = true;

                                    Application.Run(new ExtensilManager());
                                }
                            }
                        }
                    }
                    catch(Exception exc)
                    {
                        AlertBox.ShowDialog(new Exception("Due to some serious problem the application will get closed. We apologise for your inconvenience. \r\nDetails:\r\n\t" + LinkedException(exc)));
                    }
                }
                else
                {
                    Process current = Process.GetCurrentProcess();
                    foreach(Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if(process.Id != current.Id)
                        {
                            SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }
                }
            }
        }
        static string LinkedException(Exception exc)
        {
            return exc.Message + "-[" + exc.StackTrace + "]" + "\r\n\t" + (exc.InnerException == null ? "" : LinkedException(exc.InnerException));
        }
    }
}
